package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.Result;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import wiremock.com.github.jknack.handlebars.Lambda;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private OrderDetailService orderDetailService;
//    把选中的菜品或套餐加入购物车
    @PostMapping("/add")
    public Result<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
//        先设置shoppingCart的USerID，也用于后面找套餐或者菜品的数据
        shoppingCart.setUserId(BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
//        判断要添加的数据是套餐还是菜品
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper();
        lqw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        if(dishId==null){
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        else {
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
//        判断是否已存在该数据
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if(one!=null){
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingCartService.updateById(one);
        }
//        one为空，后面要对one赋shoppingCart用于返回数据
        else {
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }
        return Result.success(one);
    }

    //    把加入购物车菜品或套餐数量减少
    @PostMapping("/sub")
    public Result<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        shoppingCart.setUserId(BaseContext.getCurrentId());
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        if(dishId==null){
            lqw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        else {
            lqw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        one.setNumber(number-1);
        if(one.getNumber()==0){
            shoppingCartService.remove(lqw);
        }
        else{
            shoppingCartService.updateById(one);
        }
        return Result.success(one);
    }
    //    查看购物车
    @GetMapping("/list")
    public Result<List<ShoppingCart>> list(){
        log.info("查看购物车");
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        lqw.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return Result.success(list);
    }
//    删除购物车数据
    @DeleteMapping("/clean")
    public Result<String> delete(){
        log.info("删除购物车数据");
        LambdaQueryWrapper<ShoppingCart> lqw=new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lqw);
        return Result.success("删除成功");
    }

}
